<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>06_计算器练习</title>
</head>
<body>
    <input type="text" id="i1"> <br>
    <input type="text" id="i2"> <br>
    <button onclick="calc('+')">加</button>
    <button onclick="calc('-')">减</button>
    <button onclick="calc('*')">乘</button>
    <button onclick="calc('/')">除</button>
    <br>
    <h3>运算结果： <span class="s"></span></h3>
    <script>
        const calc = (w)=>{
            let n1 = parseFloat(document.getElementById('i1').value);
            let n2 = parseFloat(document.getElementById('i2').value);
            let spanE = document.querySelector('.s');
            switch (w){
                case '+':
                    spanE.innerHTML = n1 + n2;
                    break
                case '-':
                    spanE.innerHTML = n1 - n2;
                    break;
                case '*':
                    spanE.innerHTML = n1 * n2;
                    break;
                case '/':
                    if (n2 == 0){
                        alert('除数不能为0！')
                    }else {
                        spanE.innerHTML = n1 / n2;
                    }
                    break;
                default:
            }
        }
    </script>
</body>
</html>